package com.dtsheep.elasticsearch.es.client.document.highquery;

import com.dtsheep.elasticsearch.es.client.ElasticsearchClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

/**
 * AllQuery
 *
 * <p>
 *     全量查询
 * </p>
 *
 * @author chuanbai
 * @date 2024/5/24
 */
public class AllQuery {

    public static void main(String[] args) throws IOException {

        RestHighLevelClient client = ElasticsearchClientUtil.getEsClient();

        SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders.matchAllQuery())), RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        //条数
        System.out.println(hits.getTotalHits());
        //时间
        System.out.println(response.getTook());
        //数据
        for(SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }
    }
}
